package com.skytech.component.crawler.special;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.parse.JsoupHtmlDownloader;
import com.skytech.component.crawler.parse.SaveDataToES;
import com.skytech.component.crawler.util.ElementAbstractUrl;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class HKEXNEWS {

    @Autowired
    private SaveDataToES saveDataToES;


    public void start() {

        // ChromeDriver driver = chromeBrowser.getChromeDriver();
        // String url="http://www3.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM";

        // Arrays urls=["http://www3.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM",];
        String[] urls = {"http://www3.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM",
                "http://www3.hkexnews.hk/listedco/listconews/mainindex/SEHK_DW_DATETIME_TODAY_C.HTM",
                "http://www3.hkexnews.hk/listedco/listconews/mainindex/SEHK_OTHERS_DATETIME_TODAY_C.HTM"};

        for (String url : urls) {
            try {
                Document document = new JsoupHtmlDownloader().download(url);
                Elements allElements = document.getAllElements();
                Elements elements1 = allElements.select("body > div.content-container.content-container-reset > main > div > div > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(3) > td > table > tbody > tr.row1");
                Elements elements = allElements.select("body > div.content-container.content-container-reset > main > div > div > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(3) > td > table > tbody > tr.row0");
                elements.addAll(elements1);
                for (Element element : elements) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("publish_time", element.getElementsByTag("td").get(0).text());
                    jsonObject.put("code", element.getElementsByTag("td").get(1).text());
                    jsonObject.put("company_name", element.getElementsByTag("td").get(2).text());
                    jsonObject.put("file_source", ElementAbstractUrl.links(element.getElementsByTag("td").get(3).getElementsByTag("a")).get(0));
                    jsonObject.put("file_name", element.getElementsByTag("td").get(3).text());
                    String id = element.getElementsByTag("td").get(0).text() + " " + element.getElementsByTag("td").get(1).text();
                    id = id.split("/")[0] + id.split("/")[1] + id.split("/")[2];
                    jsonObject.put("id", id);
                    jsonObject.put("dataType", "sec");
                    jsonObject.put("sourceType","HK");
                    jsonObject.put("sourceUrl",url);
                    jsonObject.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    saveDataToES.saveData(jsonObject);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
